#include "udp.h"

extern void udp_main(const int fd, const struct sockaddr_in *addr);

void Sighandler(int sig){
	exit(0);
}

int main(int argc, const char *argv[])
{

	signal(SIGINT, Sighandler);

	if(argc < 3) {
		printf("[%s][addr][port]\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	
	int fd = socket(AF_INET, SOCK_DGRAM, 0);
	if(fd < 0)
		ErrExit("socket");

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons( atoi(argv[2]) );
	if( inet_aton(argv[1], &addr.sin_addr) == 0) {
		printf("[%s:%d] Invalid address\n", __FUNCTION__, __LINE__);
		exit(EXIT_FAILURE);
	}
	if( bind(fd, (struct sockaddr *)&addr, sizeof(addr) ) )
		ErrExit("bind");

	udp_main(fd, &addr);
	
	close(fd);
	return 0;
}

